<template>
  <div>
    <el-card>
      <CategorySelector
        :isShowList="isShowList"
        @changeCategory="changeCategory"
      ></CategorySelector>
      <!-- <CategorySelector></CategorySelector> -->
    </el-card>

    <el-card style="margin-top: 20px">
      <!-- 列表页 -->
      <div v-show="!isShowSpuForm && !isShowSkuForm">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="showAddSpuForm"
          :disabled="!category3Id"
          >添加SPU</el-button
        >
        <el-table border :data="spuList" style="width: 100%">
          <el-table-column
            align="center"
            type="index"
            prop="prop"
            label="序号"
            width="80"
          >
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row}">
              <HintButton
                type="success"
                size="mini"
                icon="el-icon-plus"
                title="添加sku"
                @click="showAddSkuForm(row)"
              ></HintButton>
              <HintButton
                type="warning"
                size="mini"
                icon="el-icon-edit"
                title="修改spu"
                @click="showUpdateSpuForm(row)"
              ></HintButton>
              <HintButton
                type="info"
                size="mini"
                icon="el-icon-info"
                title="查看spu下的sku列表"
                @click="showSkuList(row)"
              ></HintButton>

              <el-popconfirm
                :title="`你确定要删除${row.spuName}吗？`"
                @onConfirm="deleteSpu(row)"
              >
                <HintButton
                  slot="reference"
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  title="删除spu"
                ></HintButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--  @size-change="handleSizeChange"
        @current-change="handleCurrentChange" -->
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="getSpuList"
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 7]"
          :page-size="limit"
          :pager-count="7"
          layout="prev, pager, next, jumper, ->,sizes,total"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 添加或者修改spu的页面 -->
      <!-- <div></div> -->
      <!-- <SpuForm v-show="isShowSpuForm" :visible="isShowSpuForm" @update:visible="isShowSpuForm = $event"></SpuForm> -->
      <SpuForm
        ref="spu"
        v-show="isShowSpuForm"
        :visible.sync="isShowSpuForm"
        @backSuccess="backSuccess"
        @cancelBack="cancelBack"
      ></SpuForm>
      <!-- 添加sku的页面 -->
      <!-- <div></div> -->
      <!-- <SkuForm v-show="isShowSkuForm" :visible="isShowSkuForm" @update:visible="isShowSkuForm=$event"></SkuForm> -->
      <SkuForm
        ref="sku"
        v-show="isShowSkuForm"
        :visible.sync="isShowSkuForm"
      ></SkuForm>
    </el-card>

    <!-- sku列表的dialog -->
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="handlerClose"
    >
      <el-table :data="skuList" v-loading="loading">
        <el-table-column
          prop="skuName"
          label="名称"
          width="150"
        ></el-table-column>
        <el-table-column
          property="price"
          label="价格"
          width="150"
        ></el-table-column>
        <el-table-column
          property="weight"
          label="重量"
          width="200"
        ></el-table-column>
        <el-table-column property="address" label="默认图片">
          <template slot-scope="{ row }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 80px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from "@/views/product/spu/components/SkuForm";
import SpuForm from "@/views/product/spu/components/SpuForm";
export default {
  name: "SpuList",
  components: {
    SkuForm,
    SpuForm,
  },
  data() {
    return {
      page: 1,
      limit: 3,
      total: 0,
      category1Id: "",
      category2Id: "",
      category3Id: "",
      spuList: [],
      isShowList: true, //这个值这里用来控制三级联动的可操作性的，它没有控制页面的显示

      isShowSpuForm: false,
      isShowSkuForm: false,

      //这些数据是sku列表要用
      spu: {},
      dialogTableVisible: false,
      skuList: [],
      loading: true,
    };
  },

  watch:{
    isShowSpuForm(newVal,oldVal){
      this.isShowList = !newVal
    },
    isShowSkuForm(newVal,oldVal){
      this.isShowList = !newVal
    }
  },

  methods: {
    changeCategory({ categoryId, level }) {
      if (level === 1) {
        this.category3Id = "";
        this.category2Id = "";
        this.spuList = [];
        this.category1Id = categoryId;
      } else if (level === 2) {
        this.category3Id = "";
        this.spuList = [];
        this.category2Id = categoryId;
      } else {
        this.category3Id = categoryId;
        this.getSpuList();
      }
    },
    //发请求获取spu的分页列表数据
    async getSpuList(pager = 1) {
      this.page = pager;
      let { page, limit, category3Id } = this;
      try {
        const result = await this.$API.spu.getList(page, limit, category3Id);
        if (result.code === 200 || result.code === 20000) {
          this.spuList = result.data.records;
          this.total = result.data.total;
        } else {
          this.$message.error("获取分页列表失败");
        }
      } catch (error) {
        this.$message.error("获取分页列表请求失败");
      }
    },

    handleSizeChange(size) {
      this.limit = size;
      this.getSpuList();
    },

    //点击添加spu的按钮
    showAddSpuForm() {
      this.isShowSpuForm = true;
      this.$refs.spu.getInitAddSpuFormData(this.category3Id);
    },
    // 点击修改spu的按钮
    showUpdateSpuForm(row) {
      this.flag = row.id;
      this.isShowSpuForm = true;
      this.$refs.spu.getInitUpdateSpuFormData(row, this.category3Id);
    },
    //点击添加sku的按钮
    showAddSkuForm(row) {
      this.isShowSkuForm = true;
      this.$refs.sku.getInitAddSkuFormData(
        row,
        this.category1Id,
        this.category2Id
      );
    },

    // 返回成功的回调
    backSuccess() {
      //判断返回是怎么返回的
      if (this.flag) {
        //修改回来的
        this.getSpuList(this.page);
      } else {
        //添加回来的
        this.getSpuList();
      }

      this.flag = null; //重置标志位
    },
    //取消回来要清空标志位
    cancelBack() {
      this.flag = null;
    },
    //删除spu
    async deleteSpu(row) {
      try {
        const result = await this.$API.spu.remove(row.id);
        if (result.code === 200 || result.code === 20000) {
          this.$message.success("删除spu成功");
          this.getSpuList(this.spuList.length > 1 ? this.page : this.page - 1);
        } else {
          this.$message.error("删除spu失败");
        }
      } catch (error) {
        this.$message.error("删除spu请求失败");
      }
    },

    //点击查看sku列表
    async showSkuList(row) {
      this.spu = row;
      this.dialogTableVisible = true;

      //发请求获取sku列表
      this.loading = true;
      try {
        const result = await this.$API.sku.getListBySpuId(row.id);
        if (result.code === 200 || result.code === 20000) {
          this.skuList = result.data;
          this.loading = false;
        } else {
          this.$message.error("获取sku列表失败");
        }
      } catch (error) {
        this.$message.error("请求获取sku列表失败");
      }
    },
    //关闭dialog之前的回调，清空之前的所有数据
    handlerClose(){
      this.skuList = []
      this.dialogTableVisible = false
    }
  },
};
</script>

<style scoped>
</style>

